package com.beehoo.dcportal.admin.action;

import java.util.List;

import com.beehoo.dcportal.admin.service.LeaderService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.LeaderBean;

public class LeaderAction extends BaseAction {

	private static final long serialVersionUID = 4033521891435453668L;
	private LeaderService leaderService;
	private List<LeaderBean> resultList;
	private LeaderBean leaderBean;
	private FileBean fileBean;
	private String leaderId;

	public List<LeaderBean> getResultList() {
		return resultList;
	}


	public void setResultList(List<LeaderBean> resultList) {
		this.resultList = resultList;
	}


	public LeaderBean getLeaderBean() {
		return leaderBean;
	}


	public void setLeaderBean(LeaderBean leaderBean) {
		this.leaderBean = leaderBean;
	}


	public FileBean getFileBean() {
		return fileBean;
	}


	public void setFileBean(FileBean fileBean) {
		this.fileBean = fileBean;
	}


	public String getLeaderId() {
		return leaderId;
	}


	public void setLeaderId(String leaderId) {
		this.leaderId = leaderId;
	}


	public void setLeaderService(LeaderService leaderService) {
		this.leaderService = leaderService;
	}

	public String list(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			resultList = leaderService.queryLeaders(streetId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}
	
	public String initUpdate(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			leaderBean = leaderService.queryLeader(streetId, leaderId);
			super.getRequest().setAttribute("method", "update");
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return SUCCESS;
		}
		return INPUT;
	}
	
	public String update(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(leaderBean != null){
			leaderBean.setStreetId(streetId);
			leaderBean.setFileBean(fileBean);
			leaderBean.setPhotoPath(super.getText("custom.leader.img.path"));
			leaderBean.setPhotoDir(super.getText("custom.leader.img.dir"));
		}
		try {
			leaderService.updateLeader(leaderBean);
		} catch (BaseException e) {
			super.getRequest().setAttribute("method", "update");
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}
	
	public String add(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(leaderBean != null){
			leaderBean.setStreetId(streetId);
			leaderBean.setFileBean(fileBean);
			leaderBean.setPhotoPath(super.getText("custom.leader.img.path"));
			leaderBean.setPhotoDir(super.getText("custom.leader.img.dir"));
		}
		try {
			leaderService.addLeader(leaderBean);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}
	
	public String del(){
		try {
			leaderService.delLeader(leaderId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}
}
